package etp.client.gui;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

import etp.client.Tarefa;


public class TarefaStatusForm extends Composite implements StatefullForm {

	private Label lblStatus = new Label("Status:");
	
	private Label lblCompleto = new Label("Completo:");
	
	private Label lblPercent = new Label("%");
	
	private Label lblErro = new Label();
	
	private ListBox cmbStatus = new ListBox();
	
	private TextBox txtCompleto = new TextBox();
	
	private Tarefa tarefa;
	
	public TarefaStatusForm() {
		
		this.lblErro.addStyleName("ErrorLabel");
		
		FlexTable pnlMain = new FlexTable();
		
		this.initWidget(pnlMain);
		
		pnlMain.setWidget(0, 0, lblErro);
		
		pnlMain.setWidget(1, 0, lblStatus);
		
		pnlMain.setWidget(1, 1, cmbStatus);
		
		pnlMain.setWidget(2, 0, lblCompleto);
		
		pnlMain.setWidget(2, 1, txtCompleto);
	
		pnlMain.setWidget(2, 2, this.lblPercent);
		
		pnlMain.getFlexCellFormatter().setColSpan(0, 0, 3);
		pnlMain.getFlexCellFormatter().setColSpan(1, 1, 2);
		
		this.addStyleName("etp-TarefaForm");
	}

	public Tarefa getTarefa() {
		return tarefa;
	}
	
	public void setTarefa(Tarefa tarefa) {
		this.tarefa = tarefa;
	}

	public void updateFromModel(){
		this.txtCompleto.setText(this.tarefa.getPorcentagem()+"");
	}
	
	public void updateModel(Tarefa t){
		
		float percent = Float.parseFloat(this.txtCompleto.getText());
		
		t.setPorcentagem(percent);
	}
	
	public void clean(){
		this.txtCompleto.setText("");
		this.lblErro.setText("");
	}
	
	public void validate(){
		
		try{
			float porcentagem = Float.parseFloat(this.txtCompleto.getText());		
			
			if(porcentagem < 0){
				this.lblErro.setText("N\u00e3o s\u00e3o permitidos valores negativos para a porcentagem.");
				return;
			}
			
			if(porcentagem > 100){
				this.lblErro.setText("N\u00e3o s\u00e3o permitidos valores maiores que 100 para a porcentagem.");
				return;
			}
		}
		catch(NumberFormatException nfe){
			this.lblErro.setText("Porcentagem do projeto em formato inv\u00e1lido.");
			return;
		}
		
		this.lblErro.setText("");
	}
	
	@Override
	public int getFormState() {
		
		this.validate();
		
		if(this.lblErro.getText().equals("")){
			return StatefullForm.OK;
		}
		
		return StatefullForm.ERRO;
	}

}
